package lastfm
{
  import asunit.framework.TestCase;
  import lastfm.events.LastFmEvent;
  import mx.binding.utils.*;
  
  /**
   * ...
   * @author Roman Marchenko
   */
  public class TestLastFm extends TestCase
  {
    
    public function TestLastFm(testMethod:String = null)
    {
      super(testMethod);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    public function testInitInfo():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "Guns and Roses";
      var handler:Function = function(event:LastFmEvent):void
      {
        assertEquals("Guns N' Roses", event.artist);
      }
      
      charts.addEventListener(LastFmEvent.ARTIST_UPDATE, addAsync(handler, 10000));
      charts.loadInfo(artist);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    public function testInitInfoCyr():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "ддт";
      var handler:Function = function(event:LastFmEvent):void
      {
        assertEquals("ДДТ", event.artist);
      }
      
      charts.addEventListener(LastFmEvent.ARTIST_UPDATE, addAsync(handler, 20000));
      charts.loadInfo(artist);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    public function testInitInfoError():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "несуществующий_артист";
      var handler:Function = function(event:LastFmEvent):void
      {
        assertNull(event.artist);
        assertNotNull(event.error);
      }
      
      charts.addEventListener(LastFmEvent.ARTIST_NOT_FOUND, addAsync(handler, 10000));
      charts.loadInfo(artist);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    public function testLoadTrackError():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "Adele";
      charts.addEventListener(LastFmEvent.ERROR, addAsync(null, 10000));
      charts.loadSong(artist, 0);
    }
    
    ///////////////////////////////////////////////////////////////////////////
    
    public function testLoadTrack():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "Adele";
      
      var handlerArtist:Function = function(event:LastFmEvent):void
      {
        assertEquals("Adele", event.artist);
        
        var handlerTrack:Function = function(event:LastFmEvent):void
        {
          assertEquals("Adele", event.artist);
          assertEquals("Rolling in the Deep", event.song);
        }
        
        charts.addEventListener(LastFmEvent.TRACK_UPDATE, addAsync(handlerTrack, 10000));
        charts.loadSong(artist, 0);
      }
      
      charts.addEventListener(LastFmEvent.ARTIST_UPDATE, addAsync(handlerArtist, 10000));
      charts.loadInfo(artist);
    }
    
    ///////////////////////////////////////////////////////////////////////////  
    
    public function testLoadTrackAndBanned():void
    {
      var charts:LastFm = new LastFm;
      var artist:String = "Adele";

      var handlerTrack:Function = function(event:LastFmEvent):void
      {
        assertEquals("Adele", event.artist);
        assertEquals("Rolling in the Deep", event.song);
        var charts:LastFm = event.target as LastFm;
        assertFalse(charts.isBanned("Adele", "Rolling in the deep"));
        assertTrue(charts.isBanned("Korn", "A.D.I.D.A.S."));
        assertFalse(charts.isBanned("Korn", "xxx xxx"));
      }
      
      var handlerArtist:Function = function(event:LastFmEvent):void
      {
        assertEquals("Adele", event.artist);
        charts.addEventListener(LastFmEvent.TRACK_UPDATE, addAsync(handlerTrack, 30000));
        charts.loadSong(artist, 0);
      }
      
      charts.addEventListener(LastFmEvent.ARTIST_UPDATE, addAsync(handlerArtist, 20000));
      charts.loadBannedTracks("rj");
      charts.loadInfo(artist);
    }
  
  }

}
